package com.htc.controller;

import org.apache.commons.io.FilenameUtils;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.htc.model.InternalResponse;
import com.htc.model.UploadResponse;

import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.Random;

@RestController
@RequestMapping("/file")
@MultipartConfig
public class UploadImgController {

	private String nginxHost="http://localhost:8080/ssm/image";

	@PostMapping("/uploadImage")
	public InternalResponse uploadImage(@RequestParam("file") MultipartFile file,HttpServletRequest request)
			throws Exception {
        InternalResponse internalResponse;
		String uploadDirPath;
		uploadDirPath = request.getSession().getServletContext().getRealPath("/")+"/image";
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		int month =cal.get(Calendar.MONTH)+1;
		String currentMonth = year+""+(month<10?"0"+month:month);
		uploadDirPath = uploadDirPath + "/" + currentMonth;
		File imgDirFile = new File(uploadDirPath);
		if (!imgDirFile.exists()) {
			imgDirFile.mkdirs();
		}
		String trueName = file.getOriginalFilename();
		String ext = FilenameUtils.getExtension(trueName);
		String fileName = System.currentTimeMillis() + new Random().nextInt(10000) + "." + ext;
		String upload_img = uploadDirPath + "/" + fileName;
		String db_save_img = currentMonth + "/" + fileName;
		File saveFile = new File(upload_img);
		file.transferTo(saveFile);
		internalResponse = InternalResponse.success().withBody(nginxHost+ "/"+ currentMonth+ "/" + fileName);
		return internalResponse;
	}
	
	@PostMapping("/upfile")
	public void upfile(@RequestParam("upfile") MultipartFile file,HttpServletRequest request,HttpServletResponse resp)
			throws Exception {
		PrintWriter out = resp.getWriter();
        InternalResponse internalResponse;
		String uploadDirPath;
		uploadDirPath = request.getSession().getServletContext().getRealPath("/")+"/image";
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		int month =cal.get(Calendar.MONTH)+1;
		String currentMonth = year+""+(month<10?"0"+month:month);
		uploadDirPath = uploadDirPath + "/" + currentMonth;
		File imgDirFile = new File(uploadDirPath);
		if (!imgDirFile.exists()) {
			imgDirFile.mkdirs();
		}
		String trueName = file.getOriginalFilename();
		String ext = FilenameUtils.getExtension(trueName);
		String fileName = System.currentTimeMillis() + new Random().nextInt(10000) + "." + ext;
		String upload_img = uploadDirPath + "/" + fileName;
		String db_save_img = currentMonth + "/" + fileName;
		File saveFile = new File(upload_img);
		file.transferTo(saveFile);
		UploadResponse uploadResponse = new UploadResponse();
		out.print(nginxHost+ "/"+ currentMonth+ "/" + fileName);
		out.flush();
	}

	
    @RequestMapping("uploadFile.do")
    public InternalResponse upload(String un,@RequestParam("file") MultipartFile[] files, HttpServletRequest req)
            throws IllegalStateException, IOException {
    	InternalResponse internalResponse;
		String uploadDirPath;
		uploadDirPath = req.getSession().getServletContext().getRealPath("/")+"/image";
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		int month =cal.get(Calendar.MONTH)+1;
		String currentMonth = year+""+(month<10?"0"+month:month);
		uploadDirPath = uploadDirPath + "/" + currentMonth;
		File imgDirFile = new File(uploadDirPath);
		if (!imgDirFile.exists()) {
			imgDirFile.mkdirs();
		}
		String trueName = files[0].getOriginalFilename();
		String ext = FilenameUtils.getExtension(trueName);
		String fileName = System.currentTimeMillis() + new Random().nextInt(10000) + "." + ext;
		String upload_img = uploadDirPath + "/" + fileName;
		String db_save_img = currentMonth + "/" + fileName;
		File saveFile = new File(upload_img);
		files[0].transferTo(saveFile);
		internalResponse = InternalResponse.success().withBody(nginxHost+ "/"+ currentMonth+ "/" + fileName);
		return internalResponse;
    }
	


}
